using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace TechvisionUK.GML_to_RRRO
{
    public partial class ConversionForm : Form
    {
        public ConversionForm()
        {
            InitializeComponent();
        }

        private void btnConvertFromGMLToRRRO_Click(object sender, EventArgs e)
        {
            string xmlGML,  xslPath, RRROFile;
            string exceutingAsmblyPath;

            //getting executing assembly path 
            exceutingAsmblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            exceutingAsmblyPath = exceutingAsmblyPath.Substring(6);

            //Input GML XML fragment
            xmlGML = txtGML.Text;
            //if (txtGMLFile.Text.StartsWith("."))
            //{
            //    xmlGML = System.IO.File.ReadAllText(exceutingAsmblyPath + "\\" + txtGMLFile.Text.Substring(txtGMLFile.Text.IndexOf(".") + 1));
            //}
            //else
            //{
            //    xmlGML = System.IO.File.ReadAllText(txtGMLFile.Text);
            //}

            //XSL file being used for transformation
            if (txtXSLFile.Text.StartsWith("."))
            {
                xslPath = exceutingAsmblyPath + "\\" + txtXSLFile.Text.Substring(txtXSLFile.Text.IndexOf(".") + 1);
            }
            else
            {
                xslPath = txtXSLFile.Text;
            }

            //Output RRRO XML file
            if (txtRRROFile.Text.StartsWith("."))
            {
                RRROFile = exceutingAsmblyPath + "\\" + txtRRROFile.Text.Substring(txtRRROFile.Text.IndexOf(".") + 1);
            }
            else
            {
                RRROFile = txtRRROFile.Text;
            }
            

            //Transform GML into RRRO XML and display the text into text box
            txtRRRO.Text = applyXSLIntoXML(xmlGML, xslPath,RRROFile);
        }
        /// <summary>
        /// Transforms GML into RRRO XML using XSL file.
        /// </summary>
        /// <param name="xmlGMLFile">Input: GML XML file with path (absolute/relative)</param>
        /// <param name="xslFile">XSL transformation file with path (absolute/relative)</param>
        /// <param name="xmlRRROFile">Output: RRRO XML file with path (absolute/relative)</param>
        /// <returns>RRRO XML generated after tranformation</returns>
        private string applyXSLIntoXML(string xmlGMLFile, string xslFile, string xmlRRROFile)
        {
            //Creating out file for RRRO XML
            XmlWriterSettings xmlWriterStngs = new XmlWriterSettings();
            xmlWriterStngs.Encoding = Encoding.Unicode;
            
            XmlWriter writer = XmlWriter.Create(xmlRRROFile, xmlWriterStngs);
            
            //Reading input GML file 
            System.IO.StringReader stringRdr = new System.IO.StringReader(xmlGMLFile);
            XPathDocument xpathDoc = new XPathDocument(stringRdr);
            
            
            //Transforming using xsl file
            XslCompiledTransform transform = new XslCompiledTransform();
            XsltSettings xslSettings = new XsltSettings(true, true);
            //XmlResolver xmlResolver = null;
            XmlUrlResolver xmlurlResolver = new XmlUrlResolver();
            transform.Load(xslFile, xslSettings, xmlurlResolver);
            //transform.Load(xslFile);
            transform.Transform(xpathDoc, writer);
            writer.Close();
            
            //returning transformed RRRO XML text
            return (System.IO.File.ReadAllText(xmlRRROFile));
        }
        
        private void ConversionForm_Load(object sender, EventArgs e)
        {
            txtGML.Text = System.IO.File.ReadAllText(txtGMLFile.Text);
        }

        private void btnClearOutput_Click(object sender, EventArgs e)
        {
            txtRRRO.Text = string.Empty;
        }

        private void btnBrowseGML_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialogGMLFile = new OpenFileDialog();
            openFileDialogGMLFile.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*\"";
            if (openFileDialogGMLFile.ShowDialog(this) == DialogResult.OK)
            {
                txtGMLFile.Text = openFileDialogGMLFile.FileName;
                txtGML.Text = System.IO.File.ReadAllText(txtGMLFile.Text);
            }

        }

        private void btnBrowseXSL_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialogXSLFile = new OpenFileDialog();
            openFileDialogXSLFile.Filter = "xsl files (*.xsl)|*.xsl|xslt files (*.xslt)|*.xslt|All files (*.*)|*.*\"";
            if (openFileDialogXSLFile.ShowDialog(this) == DialogResult.OK)
            {
                txtXSLFile.Text = openFileDialogXSLFile.FileName;
            }
        }

        private void btnBrowseRRRO_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialogRRROFile = new OpenFileDialog();
            openFileDialogRRROFile.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*\"";
            if (openFileDialogRRROFile.ShowDialog(this) == DialogResult.OK)
            {
                txtRRROFile.Text = openFileDialogRRROFile.FileName;
            }
        }


 


    }
}